import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_verification_code/flutter_verification_code.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBLoginModule/YBLoginSubPage/YBLoginHeaderInfoPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineAuthVipModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';

class YBLoginYanZhengMaPage extends StatelessWidget {

  final String phoneN;
  final String msgToken;
  final String type;
  const YBLoginYanZhengMaPage({Key key, this.phoneN, this.msgToken,this.type}) : super(key: key);



  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text('输入验证码'),
        ),
        body: SingleChildScrollView(
          child: Padding(
              padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Container(
                    child: YBLoginHeaderInfoPage(),
                  ),
                  Container(
                    height: 30,
                  ),
                  Container(
                    child: Text("输入短信验证码",style: YBTextStyle.login1C1D1F28SemiBold,),
                  ),
                  Container(
                    height: 30,
                  ),
                  Container(
                    child: Text("已向你的手机${phoneN??""}发送短信",style: YBTextStyle.logincolor383A3D13Light,),

                  ),
                  Container(
                    height: 30,
                  ),
                  Container(
                    child:Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          child: VerificationCode(
                            textStyle: YBTextStyle.homeTitleText,
                            keyboardType: TextInputType.number,
                            underlineColor: Colors.amber,
                            itemSize: 70,
                            length: 4,
                            onCompleted: (String Value){
                                print("-----$Value--");
                                if (int.parse(type) == 1) {
                                  _loginIn(Value,context);
                                } else if (int.parse(type) == 2) {
                                  _loginBanding(msgToken, Value, phoneN, "wx_open",context);
                                }
                            },
                            onEditing: (bool value){

                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 30,
                  ),
                  Container(
                    alignment: Alignment.centerRight,
                    child: InkWell(
                      child: Text("验证码无法收到？请重新获取验证码",style: YBTextStyle.loginOtherText8F939A12,),
                      onTap: (){
                        _initLogin(phoneN, context);
                      },
                    )
                  ),
                ],
              ),
          ),
        ),
      ),
    );

  }

  //验证码获取
  void _initLogin(String phoneNumber,BuildContext context){
    YBLoginInfoApi.loginInMsgCode({"phone":phoneNumber,'sysCode':"xg_youbang"},success:(data){
      print('----$data----');
      var msgTokenMap = data;
      //msgToken
      print('msgToken---$msgTokenMap');
      if (msgTokenMap['msgToken'].toString().isNotEmpty) {
      //   _msgT = msgTokenMap['msgToken'];
      }
    },fail: (error){
      print("---$error---");
    });
  }


  void _loginIn(String yanCode,BuildContext context){
    EasyLoading.show(status: '登录中...');
    YBLoginInfoApi.loginInPhone(
      {"customerPhone":phoneN,"msgCode":yanCode,"sysCode":"xg_youbang","msgToken":msgToken},
      success:(data){
        print("object----$data-");
        EasyLoading.dismiss();
        SpUtil.putString("token",data);
        print("-存值是成功了--${SpUtil.putString("token",data)}");
        print("--存在数据--${SpUtil.getString("token")}");

        EasyLoading.showSuccess('登录成功',duration: Duration(seconds: 1));

        Future.delayed(Duration(seconds: 0),(){
          YBNavigatorUtil.goToHomeRemovePage(context);
        });

      },fail: (error){
        print(error);
    });
  }

  void _loginBanding(String msgtokenStr,String msgCodeStr,String phone,String type,BuildContext context){
    YBLoginInfoApi.loginInFirstPhone({
      "sysCode":"xg_youbang",
      "msgToken":msgtokenStr,
      "msgCode":msgCodeStr,
      'customerPhone':phone,
      "type":type,
    },success: (data){
      print('---objet-$data-');
      var dataStr = data;
      if (dataStr != null){
        SpUtil.putString("token",dataStr);
        _isPhoneNumberDisplay(context);
      }
    },fail: (error){
      print('---objet-$error-');
    });
  }

  static void _isPhoneNumberDisplay(BuildContext context){
    YBMineNetWork.mineUserInfoData({
      "sysCode":"xg_newyijiwang",
    },success:(data){
      print('--个人信息---$data--');
      EasyLoading.showSuccess('登录成功',duration: Duration(seconds: 1));
      var mineUserInfoModel = YBMineUserInfoModel.fromJson(data);
      if (mineUserInfoModel.customerAuthorities != null) {
        for (YBMineAuthVipModel mineAuthModel in mineUserInfoModel
            .customerAuthorities) {
          if (mineAuthModel.authority.contains('BLVIP')) {
            SpUtil.putString("VIP", "1");
            break;
          }
        }
      }
      Future.delayed(Duration(seconds: 0),(){
        YBNavigatorUtil.goToHomeRemovePage(context);
      });
    },fail: (error){
      print('--$error-');

    });
  }


}
